package com.atguigu.guli.service.vod.service.impl;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.guli.service.vod.service.UploadService;
import com.atguigu.guli.service.vod.util.VODProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Service
public class UploadServiceImpl implements UploadService {
    @Autowired
    VODProperties vodProperties;

    //返回的map集合中有前端需要的vid属性及playauth属性
    @Override
    public Map upload(MultipartFile multipartFile)  {
        String filename = multipartFile.getOriginalFilename();
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        UploadStreamRequest request = new UploadStreamRequest(vodProperties.getKeyId(), vodProperties.getKeySecret(), filename, filename, inputStream);
        //设置工作流id
        request.setWorkflowId(vodProperties.getWorkflowId());
        request.setTemplateGroupId(vodProperties.getTemplateGroupId());
        UploadVideoImpl uploadVideo = new UploadVideoImpl();
        UploadStreamResponse response = uploadVideo.uploadStream(request);
        String videoId = response.getVideoId();
        String playauth = null;
        try {
            playauth = getPlayauth(videoId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("vid",videoId);
        map.put("playauth",playauth);
        return map;
    }

    @Override
    public Map getPlayAuth(String id)  {
        System.out.println("id = " + id);
        String playauth = getPlayauth(id);
        Map map = new HashMap();
        map.put("item",playauth);
        return map;
    }

    //查询请求返回的playauth属性
    private   String getPlayauth(String ID)  {
        //1、创建客户端对象
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId,
                vodProperties.getKeyId(), vodProperties.getKeySecret());
        DefaultAcsClient client = new DefaultAcsClient(profile);
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        try {
            //2、使用客户端根据请求信息 发送请求
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(ID);
            request.setAuthInfoTimeout(3000L);//配置加密视频凭证的过期时间为3000秒  默认100秒
            response = client.getAcsResponse(request);
            //播放凭证
            return response.getPlayAuth();
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }
    }
